home *** CD-ROM | disk | FTP | other *** search
/ Maclife 13 / MACLIFE13-No-93-1996.ISO.7z / MACLIFE13-No-93.ISO / Programming / CodeWarrior Sample Source / MyDemo.(8) / MyDemo.c next >
C/C++ Source or Header  |  1996-03-19  |  9KB  |  294 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.c    -    C Source
  10. #
  11. #    Copyright ゥ 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    SillyBalls.c        August 1, 1988
  17. #                SillyBalls.make        August 1, 1988
  18. #
  19. #    This is a very simple sample program that demonstrates how to use Color 
  20. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  21. #    a color window and draw randomly colored ovals in the window.
  22. #    
  23. #    The purpose is to show how to get some initial results with Color QuickDraw.
  24. #    It is a complete program and is very short to be as clear as possible.
  25. #    
  26. #    It does not have an Event Loop.  It is not fully functional in the sense that
  27. #    it does not do all the things you would expect a well behaved Macintosh 
  28. #    program to do, like size the window naturally, have an event loop, use menus, 
  29. #    etc.
  30. #
  31. #    See Sample and TESample for the general structure and MultiFinder techniques that
  32. #    we recommend that you use when building a new application.
  33. #
  34. ------------------------------------------------------------------------------*/
  35.  
  36.     
  37. //    Version 1.0:    6/2/88
  38. //                    7/20/88     DJB    Converted to C
  39. //    
  40. //    purpose        To demonstrate a simple color App using Color QuickDraw.
  41. //                        It draws colored balls in a color window, then uses colored
  42. //                        text inverted in the ball.  The ball location and color is Random.
  43. //                        
  44. //                        This program was written by Bo3b Johnson, 1/88.
  45. //                        
  46. //                        The inverted Bob text was a Skippy Blair special concept,
  47. //                        kept for obvious aesthetic reasons.
  48.  
  49. //MW -cut out some other program descriptions.-
  50.  
  51. //MW ** Metrowerks note **
  52. //   All changed code by Metrowerks is commented by "//MW".
  53. //   There is one type of modification to the original source:
  54. //   ・ Added argument type and return type to function definitions.
  55. //       In order to pass with extended error checking on.
  56. //    
  57. //   8/31/93 JA
  58.  
  59.  
  60. #include <Types.h>
  61. #include <Memory.h>
  62. #include <Quickdraw.h>
  63. #include <Fonts.h>
  64. #include <Events.h>
  65. #include <Menus.h>
  66. #include <Windows.h>
  67. #include <TextEdit.h>
  68. #include <Dialogs.h>
  69. #include <OSUtils.h>
  70. #include <ToolUtils.h>
  71. #include <SegLoad.h>
  72.  
  73. /* Constants */
  74. #define BallWidth        20
  75. #define BallHeight        20
  76. #define BobSize            8        /* Size of text in each ball */
  77.  
  78. /* Globals */
  79. Rect    windRect;
  80.     
  81. /* Prototypes */
  82. void Initialize(void);
  83. void NewBall(void);
  84. void EventLoop(void);    // ゼロから始めるCodeWarrior (6)
  85. void DoAbout(void);        // ゼロから始めるCodeWarrior (7)
  86.  
  87. // 
  88. //    Main body of program SillyBalls
  89. //
  90.  
  91. //MW specified argument and return type.
  92. void main(void)
  93. {
  94.     Initialize();
  95.     EventLoop();
  96. //    do {
  97. //        NewBall();
  98. //    } while (!Button());
  99.     
  100. }
  101.  
  102. // 
  103. //    Initialize everything for the program, make sure we can run
  104. //
  105. MenuHandle menu1,menu2;
  106. //MW specified argument and return type.
  107. void Initialize(void)
  108. {
  109.     WindowPtr    mainPtr;
  110.     OSErr        error;
  111.     SysEnvRec    theWorld;
  112.     MenuHandle    menuH;    // ゼロから始めるCodeWarrior (8)
  113.         
  114.     //
  115.     //    Test the computer to be sure we can do color.  
  116.     //    If not we would crash, which would be bad.  
  117.     //    If we canユt run, just beep and exit.
  118.     //
  119.  
  120.     error = SysEnvirons(1, &theWorld);
  121.     if (theWorld.hasColorQD == false) {
  122.         SysBeep(50);
  123.         ExitToShell();                    /* If no color QD, we must leave. */
  124.     }
  125.     
  126.     /* Initialize all the needed managers. */
  127.     InitGraf(&qd.thePort);
  128.     InitFonts();
  129.     InitWindows();
  130.     InitMenus();
  131.     TEInit();
  132.     InitDialogs(nil);
  133.     InitCursor();
  134.  
  135.     //
  136.     //    To make the Random sequences truly random, we need to make the seed start
  137.     //    at a different number.  An easy way to do this is to put the current time
  138.     //    and date into the seed.  Since it is always incrementing the starting seed
  139.     //    will always be different.  Donユt for each call of Random, or the sequence
  140.     //    will no longer be random.  Only needed once, here in the init.
  141.     //
  142.     GetDateTime((unsigned long*) &qd.randSeed);
  143.  
  144.     SetMenuBar(GetNewMBar(128));
  145.     menuH = GetMenuHandle(128);        // 128=AppleメニューのID, ゼロから始めるCodeWarrior (8)
  146.     AppendResMenu(menuH, 'DRVR');    // ゼロから始めるCodeWarrior (8)
  147.     DrawMenuBar();
  148.     //
  149.     //    Make a new window for drawing in, and it must be a color window.  
  150.     //    The window is full screen size, made smaller to make it more visible.
  151.     //
  152.     windRect = qd.screenBits.bounds;
  153.     InsetRect(&windRect, 50, 50);
  154.     mainPtr = NewCWindow(nil, &windRect, "¥pBob Land", true, documentProc, 
  155.                         (WindowPtr) -1, false, 0);
  156.         
  157.     SetPort(mainPtr);                        /* set window to current graf port */
  158.     TextSize(BobSize);                        /* smaller font for drawing. */
  159.  
  160. }
  161.  
  162.  
  163. // 
  164. //    NewBall: make another ball in the window at a random location and color.
  165. //
  166.  
  167. //MW -specified argument and return type.-
  168. void NewBall(void)
  169. {
  170.     RGBColor    ballColor;
  171.     Rect        ballRect;
  172.     long int    newLeft,
  173.                 newTop;
  174.     
  175.     // 
  176.     //    Make a random new color for the ball.
  177.     //
  178.     ballColor.red   = Random();
  179.     ballColor.green = Random();
  180.     ballColor.blue  = Random();
  181.     
  182.     // 
  183.     //    Set that color as the new color to use in drawing.
  184.     //
  185.     RGBForeColor (&ballColor);
  186.  
  187.     //    
  188.     //    Make a Random new location for the ball, that is normalized to the window size.  
  189.     //    This makes the Integer from Random into a number that is 0..windRect.bottom
  190.     //    and 0..windRect.right.  They are normalized so that we don't spend most of our
  191.     //    time drawing in places outside of the window.
  192.     //
  193.     newTop = Random();    newLeft = Random();
  194.     newTop = ((newTop+32767) * windRect.bottom)/65536;
  195.     newLeft = ((newLeft+32767) * windRect.right)/65536;
  196.     SetRect(&ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  197.     
  198.     //
  199.     //    Move pen to the new location, and paint the colored ball.
  200.     //
  201.     MoveTo(newLeft, newTop);
  202.     PaintOval (&ballRect);
  203.     
  204.     //
  205.     //    Move the pen to the middle of the new ball position, for the text
  206.     //
  207.     MoveTo(ballRect.left + BallWidth/2 - BobSize, 
  208.         ballRect.top + BallHeight/2 + BobSize/2 -1);
  209.     
  210.     //    
  211.     //    Invert the color and draw the text there.  This wonユt look quite right in 1 bit
  212.     //    mode, since the foreground and background colors will be the same.
  213.     //    Color QuickDraw special cases this to not invert the color, to avoid
  214.     //    invisible drawing.
  215.     //
  216.     InvertColor(&ballColor); 
  217.     RGBForeColor(&ballColor);
  218.     DrawString("¥pBob");
  219. }
  220.  
  221.  
  222. void EventLoop(void)    // ゼロから始めるCodeWarrior (6)(7)(8)
  223. {
  224.     EventRecord event;
  225.     Boolean    quit = false;
  226.     long    selectID, menuID, itemID;
  227.     short    DAstatus;    // 使わない状態値, ゼロから始めるCodeWarrior (8)
  228.     Str255 DAname;    // ゼロから始めるCodeWarrior (8)
  229.     
  230.     do {
  231.         if (WaitNextEvent(everyEvent, &event, 10, 0)) {
  232.             switch(event.what) {
  233.             case mouseDown:
  234.                 if (selectID = MenuSelect(event.where)) {
  235.                     menuID = HiWord(selectID);
  236.                     itemID = LoWord(selectID);
  237.                     if (menuID == 129 && itemID == 1)
  238.                         quit = true;
  239.                     else if (menuID == 128)    // ゼロから始めるCodeWarrior (7)(8)
  240.                     {
  241.                                 // About画面のメニュー処理
  242.                         if (itemID == 1)        // ゼロから始めるCodeWarrior (7)
  243.                             DoAbout();    // ゼロから始めるCodeWarrior (7)
  244.                                 // アップルメニューアイテムの選択処理
  245.                         else if (itemID > 2) {    // ゼロから始めるCodeWarrior (8)
  246.                             GetMenuItemText(GetMenuHandle(128), itemID, DAname);
  247.                             DAstatus = OpenDeskAcc(DAname);
  248.                         }
  249.                     }
  250.                     HiliteMenu(0);
  251.                 }
  252.                 break;
  253.             case keyDown:
  254.                 break;
  255.             default:
  256.                 NewBall();
  257.                 break;
  258.             }
  259.         }
  260.     } while (!quit);
  261. }
  262.  
  263. void DoAbout(void)    // ゼロから始めるCodeWarrior (7)
  264. {
  265.     WindowPtr    aboutPtr, oldPort;
  266.     Rect    aboutWindRect, thePictRect, pictRect;
  267.     PicHandle    thePicture;
  268.     short    theWidth, theHeight;
  269.  
  270.                     // about画面のPICTリソース(ID=128)と同じ大きさとし,ウィンドウの表示位置を(100,100)とする    
  271.     SetRect(&aboutWindRect, 100,100,256+100,128+100);
  272.                     // ウィンドウのタイトルをアプリケーション名とする
  273.     aboutPtr = NewCWindow(nil, &aboutWindRect, "¥pデモでもデモ について", true, documentProc, 
  274.                         (WindowPtr) -1, false, 0);
  275.  
  276.     GetPort(&oldPort);    // 直前のグラフポートを保存する
  277.     SetPort(aboutPtr);    // グラフポートをabout画面のグラフポートへ変更する
  278.     
  279.     thePicture = GetPicture(128);    // about画面のPICTリソース(ID=128)を読み込み,
  280.     thePictRect = (**(thePicture)).picFrame;    // PICTの表示位置を求める
  281.     theWidth = thePictRect.right - thePictRect.left;
  282.     theHeight = thePictRect.bottom - thePictRect.top;
  283.     SetRect(&pictRect, 0, 0, theWidth, theHeight);
  284.     DrawPicture(thePicture, &pictRect);    // PICTをabout画面に表示する
  285.     ReleaseResource( (Handle) thePicture );    // PICTリソースのメモリ領域を解放する
  286.     
  287.     do {    // クリックされるまでabout画面を表示し続ける
  288.     } while (!Button());
  289.     
  290.     DisposeWindow(aboutPtr);    // about画面用のメモリ領域を解放する=ウィンドウを消す
  291.     SetPort(oldPort);    // グラフポートを直前のグラフポートへ戻す
  292. }
  293.  
  294.